how to use an adress as function parameter (bis)

Hy,

In C, I always did a such function :

void myFree( myType ** i_pt)
{
if (*pt!=NULL)
{
free(*i_pt);
*i_pt=NULL;
}
}

In DXL I would like to do something like :

void myClose(Module & i_mod)
{
if (*i_mod!=null)
{
close(i_mod)
i_mod=null
}
}

but the syntax if (*i_mod!=null) does not exist in DXL
so how to do ?

Tahnks in advance for any help

Sincerly,

Bernard
bgr - Wed Sep 01 09:55:52 EDT 2010

Re: how to use an adress as function parameter (bis)
bgr - Wed Sep 01 10:30:40 EDT 2010

I found a solution, is it a right one ?

bool aClose( Module m )
{
if (null!=m)
{
close(m)
return true
}
else
{

}
}

Re: how to use an adress as function parameter (bis)
bgr - Wed Sep 01 10:36:35 EDT 2010

I found a solution, is it a right one ?

bool aClose( Module m )
{
if (null!=m)
{
close(m)
return true
}
else
{
return false
}
}

void aSafeClose( Module & m)
{
bool flag=aClose(m)
if(true==flag)
{
m=null
}
}

void aFunc()
{
Module m=edit("a full path")
...
...
...
aSafeClose(m)
}

Re: how to use an adress as function parameter (bis)
Mathias Mamsch - Thu Sep 02 02:55:56 EDT 2010

bgr - Wed Sep 01 10:36:35 EDT 2010
I found a solution, is it a right one ?

bool aClose( Module m )
{
if (null!=m)
{
close(m)
return true
}
else
{
return false
}
}

void aSafeClose( Module & m)
{
bool flag=aClose(m)
if(true==flag)
{
m=null
}
}

void aFunc()
{
Module m=edit("a full path")
...
...
...
aSafeClose(m)
}

First of all, you can use pointers in DXL much like in C. Responsible for this are the perms:
 

_k* ::(&) (_k&)  // the & operator makes a pointer from a reference or a variable
_k& ::(*) (_k*)  // the * operator makes a reference from a pointer

 


Unfortunately you can only make a reference out of a pointer not the value, therefore you can not directly dereference a reference or a pointer, e.g. for a null check. Nevertheless you can do stuff like this:

 

 

int a = 4
int *ptr = &a 
int &aRef = a
int *ptr2 = &a
int *ptr3 = &aRef
print (*ptr)
print ptr
int **ptrptr = &ptr
print ptrptr
print (**ptrptr)



Regarding the "SafeClose", i guess a simple:



 

 

void safeClose(Module &m) { Module x = m; if (!null x) { close m; m = null } }



would have done it. Do dereference a reference (for example before a null check, just assign it to a normal variable). Hope that clears things up.

Regards, Mathias



 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS